<?php


namespace app\api\validate;


use app\lib\exception\parameterException;

class orderPlace extends baseValidate {
    //前端传入数据的格式
    protected $array = array(
        array('product_id'=>1,'count'=>3),
        array('product_id'=>1,'count'=>3),
        array('product_id'=>1,'count'=>3),
    );


    protected $rule = [
        'products' =>'checkProducts'
    ];


    protected $singleRule = [
        'product_id'=>'require|isInteger',
        'count'=>'require|isInteger'
    ];


    protected function checkProducts($data){
        if(!is_array($data)){
            throw new parameterException(['msg'=>'商品参数不正确']);
        }

        if(empty($data)){
            throw new parameterException(['msg'=>'商品列表不能为空']);
        }

        foreach ($data as $value){
            $this->checkProduct($value);
        }

        return true;
    }


    //验证二位数组中的每个子数组
    protected function checkProduct($data){
        $validate = new baseValidate($this->singleRule);
        $result = $validate->check($data);
        if(!$result){
            throw new parameterException(['msg'=>'商品参数列表错误']);
        }

        return true;

    }

}

